package com.dream.keigezhushou.Activity.Fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.Uiutils.UiUtils;
import com.dream.keigezhushou.Activity.bean.KTVInfo;
import com.dream.keigezhushou.R;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;

import static com.dream.keigezhushou.Activity.Uiutils.UiUtils.inflateView;
import static com.dream.keigezhushou.R.layout.ktvbuyfragment_head;

/**
 * Created by kaifa on 2017/2/24.
 */
public class BuyFragment extends Fragment implements View.OnClickListener {
    private ArrayList<KTVInfo> data = new ArrayList<>();
    private ImageView mIv;
    private ImageView mIv1;
    private ImageView mIv2;
    private ImageView mIv3;
    private TextView mNearby;
    private ListView mLvlist;
    private PullToRefreshListView mMPullToRefreshListView;
    private View mHeadView;


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.image_bg:
                UiUtils.toast("跳转页面");
                break;
            case R.id.image_1:
                UiUtils.toast("跳转页面");
                break;
            case R.id.image_2:
                UiUtils.toast("跳转页面");
                break;
            case R.id.image_3:
                UiUtils.toast("跳转页面");
                break;
            case R.id.nearby:
                UiUtils.toast("跳转页面");
                break;
        }

    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        mMPullToRefreshListView = (PullToRefreshListView) UiUtils.inflateView(R.layout.ktvbuyfragment);

        mHeadView = UiUtils.inflateView(ktvbuyfragment_head);
        mLvlist = mMPullToRefreshListView.getRefreshableView();
        mLvlist.addHeaderView( mHeadView);

        mIv = (ImageView) mHeadView.findViewById(R.id.image_bg);
        mIv1 = (ImageView) mHeadView.findViewById(R.id.image_1);
        mIv2 = (ImageView) mHeadView.findViewById(R.id.image_2);
        mIv3 = (ImageView) mHeadView.findViewById(R.id.image_3);
        mNearby = (TextView) mHeadView.findViewById(R.id.nearby);

        mIv.setOnClickListener(this);
        mIv1.setOnClickListener(this);
        mIv2.setOnClickListener(this);
        mIv3.setOnClickListener(this);
        mNearby.setOnClickListener(this);

        initData();//假数据




        //getDataFromService();   获取网络数据并解析
        // 加载动画
       //设置上拉刷新重新请求网络数据  更新adapter?




        mLvlist.setAdapter(new MyAdapter(data));
        mLvlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                UiUtils.toast("跳到KTV详情页");
//                Intent intent = new Intent(UiUtils.getContext(), GoodsDetailActivity.class);//Ktv详情页面
//                intent.putExtra("position", position);
//             //   intent.putExtra("url", url);
//                startActivity(intent);
            }
        });
        return mMPullToRefreshListView;

    }

    private void initData(){
        KTVInfo info = new KTVInfo();
        info.setDistance("0.5km");
        info.setIv(getResources().getDrawable(R.mipmap.icon_iv));
        info.setLocation("未央路");
        info.setTitle("欢唱KTV");
        info.setPrivilege("39元/券，6小时欢唱+小吃饮品套餐");
        info.setOrder_detailsorder_details("开心包6小时68元，乐唱包6小时68元");
        info.setPirce("40");
        info.setPingfen("4");
        data.add(info);
        KTVInfo info2 = new KTVInfo();
        info2.setDistance("0.5km");
        info2.setIv(getResources().getDrawable(R.mipmap.icon_iv));
        info2.setLocation("未央路");
        info2.setTitle("欢唱KTV");
        info2.setPrivilege("39元/券，6小时欢唱+小吃饮品套餐");
        info2.setOrder_detailsorder_details("开心包6小时68元，乐唱包6小时68元");
        info2.setPirce("40");
        info2.setPingfen("4");
        data.add(info2);
        KTVInfo info3 = new KTVInfo();
        info3.setDistance("0.5km");
        info3.setIv(getResources().getDrawable(R.mipmap.icon_iv));
        info3.setLocation("未央路");
        info3.setTitle("欢唱KTV");
        info3.setPrivilege("39元/券，6小时欢唱+小吃饮品套餐");
        info3.setOrder_detailsorder_details("开心包6小时68元，乐唱包6小时68元");
        info3.setPirce("40");
        info3.setPingfen("4");
        data.add(info3);
        KTVInfo info4 = new KTVInfo();
        info4.setDistance("0.5km");
        info4.setIv(getResources().getDrawable(R.mipmap.icon_iv));
        info4.setLocation("未央路");
        info4.setTitle("欢唱KTV");
        info4.setPrivilege("39元/券，6小时欢唱+小吃饮品套餐");
        info4.setOrder_detailsorder_details("开心包6小时68元，乐唱包6小时68元");
        info4.setPirce("40");
        info4.setPingfen("4");
        data.add(info4);
    }
    class MyAdapter extends BaseAdapter {


        public MyAdapter(ArrayList<KTVInfo> datalist) {
            data = datalist;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public KTVInfo getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder=null;
            if(convertView==null){
                convertView= inflateView(R.layout.item_ktvbuy_fragment);
                holder = new ViewHolder();
                holder.iv = (ImageView) convertView.findViewById(R.id.image_iv);
                holder.title = (TextView) convertView.findViewById(R.id.ktv_title);
                holder.pingfen = (TextView) convertView.findViewById(R.id.pingfen);
                holder.bar = (RatingBar)convertView.findViewById(R.id.ratingBar);
                holder.pirce = (TextView) convertView.findViewById(R.id.price);
                holder.location = (TextView) convertView.findViewById(R.id.location);
                holder.distance = (TextView) convertView.findViewById(R.id.distance);
                holder.order_detailsorder_details = (TextView) convertView.findViewById(R.id.order_details);
                holder.privilege = (TextView) convertView.findViewById(R.id.privilege );
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

            KTVInfo info = getItem(position);
            holder.iv.setImageDrawable(info.getIv());
            holder.title.setText(info.getTitle());
            holder.pingfen.setText(info.getPingfen());
            holder.bar.setRating(Float.parseFloat(info.getPingfen()));
            holder.pirce.setText(info.getPirce());
            holder.location.setText(info.getLocation());
            holder.distance.setText(info.getDistance());
            holder.order_detailsorder_details.setText(info.getOrder_detailsorder_details());
            holder.privilege.setText(info.getPrivilege());


            return convertView;
        }
    }

    static class ViewHolder {
        ImageView iv;
        TextView title;
        TextView pirce;
        TextView pingfen;
        TextView location;
        TextView distance;
        TextView order_detailsorder_details;
        TextView privilege;
        RatingBar bar;
    }




}
